package com.studies.pc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studies.pc.mapper.ActivityMapper;
import com.studies.pc.pojo.Activity;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studies.pc.service.ActivityService;
import com.studies.pc.vo.ActivityDetailsVo;
import com.studies.pc.query.ActivityQuery;
import com.studies.pc.vo.ActivityschyearVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 研学活动 服务实现类
 * </p>
 *
 * @author 研学活动后台
 * @since 2019-12-24
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {


    @Override
    public Map<String, Object> selectPage(Page<Activity> pageParam, ActivityQuery activityQuery) {
        QueryWrapper<Activity> queryWrapper =new QueryWrapper<>();
          queryWrapper.orderByDesc("update_time");
        if (activityQuery.getBaseName()!=null){
            queryWrapper.eq("base_name",activityQuery.getBaseName());
        }
       if (activityQuery.getUpdateTime()!=null){
            queryWrapper.between("sign_start_time",activityQuery.getSerStartTime(),activityQuery.getSignEndTime());
        }
        baseMapper.selectPage(pageParam,queryWrapper);
        List<Activity> records =pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
//废弃方法
//    @Override
//    public IPage<Activity> selectPage(Page<Activity> activityPage, ActivityVo activityVo) {
//        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
//        try{
//            queryWrapper.orderByDesc("update_time");
//            if (activityVo.getBaseName() != null) {
//                //按name查询
//                queryWrapper.eq("base_name",activityVo.getBaseName());
//            }
//            if (activityVo.getUpdateTime()!=null){
//                queryWrapper.between("ser_start_time",activityVo.getSerStartTime(),activityVo.getSignEndTime());
//                //按时间查询
//            }
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            return baseMapper.selectPage(activityPage, queryWrapper);
//        }

    @Override
    public ActivityDetailsVo activityDetails(Integer id) {
        //活动详情
        ActivityDetailsVo activityDetailsVo = baseMapper.activityDetails(id);
        return activityDetailsVo;
    }

    @Override
    public ActivityschyearVo getAllSchYear() {
        ActivityschyearVo allSchYear = baseMapper.getAllSchYear();
        return allSchYear;
    }

    @Override
    public Object getActivityStatisticsCount(String schyear) {
        QueryWrapper<Activity> queryWrapper =new QueryWrapper<>();
        if (schyear!=null){
            queryWrapper.eq("update_time",schyear);
        }

        return null;
    }

}
